﻿#region Using

using Common.Base;
using Common.Enums;

using GameCommon.Args;
using GameCommon.Base;
using GameCommon.Manager;
using GameCommon.ManagerInterfaces;
using GameCommon.Services;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

#endregion

namespace GameCommon.Components
{
    public class ToolTip : GameComponent
    {
        private Texture2D texture2D;

        public ToolTip(string id)
            : base(id)
        {
            this.FontSize = FontSizeEnum.S10;
            this.FontEnum = FontEnum.Calibri;

            Managers.MouseManager.MouseMoved += this.OnMouseMoved;
        }

        public Color BackgroundColor { get; set; }

        public string Value { get; set; }

        public Dimension TextDimension { get; set; }

        public Font Font { get; private set; }

        public FontEnum FontEnum { get; set; }

        public FontSizeEnum FontSize { get; set; }

        public int LineHeight { get; set; }

        public int Padding { get; set; }

        public HorizontalAlignment HorizontalAlignment { get; set; }

        public VerticalAlignment VerticalAlignment { get; set; }

        public override void Draw(IDrawingManager manager, GameTime gameTime)
        {
            this.DrawingService.Draw(manager, this.texture2D, this.Dimension, this.Opacity, this.Layer);
            this.DrawingService.DrawFont(manager, this.Font, this.TextDimension, this.Value, this.Opacity, this.Layer + 1);
        }

        public override void Settle()
        {
            base.Settle();

            this.Font = Managers.FontManager.GetFont(this.FontEnum, this.FontSize);
            this.Dimension = new Dimension(0, 0, Service.FontService.GetSize(this.Value, this.Font) + this.Padding);
            this.TextDimension = new Dimension(0, 0, Service.FontService.GetSize(this.Value, this.Font));
            this.texture2D = Managers.ContentManager.GetRectangle(this.BackgroundColor);
        }

        public override string ToString()
        {
            return string.Format("Tooltip, Id: {0}", this.Id);
        }

        public override void Dispose()
        {
            base.Dispose();
            Managers.MouseManager.MouseMoved -= this.OnMouseMoved;
        }

        private void OnMouseMoved(MouseMovedArgs args)
        {
            this.Dimension.X = args.Position.X + 15;
            this.Dimension.Y = args.Position.Y + 15;

            Service.ComponentModifier.ApplyAlignments(this.TextDimension, this.Dimension, this.Offset, this.HorizontalAlignment, this.VerticalAlignment);
        }
    }
}